//
//  UnlimitedHorizontalScrollView.m
//  baovn
//
//  Created by Tai Le on 11/20/14.
//  Copyright (c) 2014. All rights reserved.
//

#import "UnlimitedHorizontalScrollView.h"
#import "UnlimitedHorizaontalItemView.h"

#define VISIBLE_ITEMS 3
#define MAX_PAGES 999

@implementation UnlimitedHorizontalScrollView

- (instancetype)init
{
    if (self = [super init])
    {
        _scrollView = [[UIScrollView alloc] init];
        _scrollView.pagingEnabled = YES;
        _scrollView.showsHorizontalScrollIndicator = NO;
        _scrollView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
        _scrollView.delegate = self;
        [self addSubview:_scrollView];
        
        _visibleItems = [[NSMutableArray alloc] init];
    }
    
    return self;
}

- (void)setFrame:(CGRect)frame
{
    [super setFrame:frame];
    
    _scrollView.frame = self.bounds;
    _scrollView.contentSize = CGSizeMake(_scrollView.width * MAX_PAGES, 0);
    
    [self updateChildFrames];
}

- (void)updateChildFrames
{
    for (UnlimitedHorizaontalItemView *view in _visibleItems)
    {
        view.frame = RECT(view.indexItem * _scrollView.width, 0, _scrollView.width, _scrollView.height);
    }
    
    _scrollView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
}

- (void)reloadData
{
    if (_selectedIndex < 0 || _selectedIndex >= MAX_PAGES)
    {
        return;
    }
    
    [self clearItems];
    
    _scrollView.contentOffset = CGPointMake(_selectedIndex * _scrollView.width, 0);
    
    [self addItemForIndex:_selectedIndex - 1];
    [self addItemForIndex:_selectedIndex];
    [self addItemForIndex:_selectedIndex + 1];
}

- (void)addItemForIndex:(NSInteger)idex
{
    if (idex < 0 || idex >= MAX_PAGES)
    {
        return;
    }
    
    UnlimitedHorizaontalItemView *item = [_delegate unlimitedHorizontalScrollView:self cellForItemAtIndex:idex];
    item.indexItem = idex;
    item.frame = RECT(idex * _scrollView.width, 0, _scrollView.width, _scrollView.height);
    [item setNeedsLayout];
    [_scrollView addSubview:item];
    [_visibleItems addObject:item];
    
    NSLog(@"add item at %ld", idex);
}

- (void)removeItemForIndex:(NSInteger)idex
{
    for (UnlimitedHorizaontalItemView *itemView in _visibleItems)
    {
        if (itemView.indexItem == idex)
        {
            [itemView removeFromSuperview];
            [_visibleItems removeObject:itemView];
            
            NSLog(@"remove item at %ld", idex);
            
            break;
        }
    }
}

- (void)clearItems
{
    for (UnlimitedHorizaontalItemView *itemView in _visibleItems)
    {
        [itemView removeFromSuperview];
    }
    
    [_visibleItems removeAllObjects];
}

- (void)checkAddRemoveItem
{
    NSInteger offsetX = _scrollView.contentOffset.x;
    NSInteger curIndex = offsetX/_scrollView.width;
    
    if (curIndex > _selectedIndex)
    {
        [self addItemForIndex:_selectedIndex + 2];
        [self removeItemForIndex:_selectedIndex - 1];
    }
    else if (curIndex < _selectedIndex)
    {
        [self addItemForIndex:_selectedIndex - 2];
        [self removeItemForIndex:_selectedIndex + 1];
    }
    
    _selectedIndex = curIndex;
}

#pragma mark - Delegate
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView
{
    _scrollView.userInteractionEnabled = NO;
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    _scrollView.userInteractionEnabled = YES;
    
    [self checkAddRemoveItem];
}

@end
